﻿using OccurrencesCore.Model;
using OccurrencesCore.Service;
using OccurrencesWeb.Page;
using OccurrencesWorkflow.BaseControl.Base;
using OccurrencesWorkflow.SpecializedControl.ScheduledReturn;
using OccurrencesWorkflow.SpecializedControl.ScheduledReturn.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OccurrencesWeb
{
    public partial class _Default : ExtendedPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CarrierService service = new CarrierService();
            List<Carrier> list = service.FindAll();
            foreach (Carrier carrier in list)
            {
                lit.Text += carrier.Name + ",";
            }
        }

        protected void btnCreateWorkflow_Click(object sender, EventArgs e)
        {
            string name = txtNomeOcorrencia.Text;

            UserProfileService userProfileService = new UserProfileService();
            UserProfile userProfile = new UserProfile()
            {
               Name = "Admin",
               Acronym="ad"               
            };
            userProfileService.Create(userProfile);

            UserService userService = new UserService();
            User user = new User()
            {
                Active = true,
                Email = string.Empty,
                Login = "leonardoj",
                Name = "Leonardo",
                UserProfileId = 1,
                UserType = string.Empty
            };
            userService.Create(user);

            CarrierService carrierService = new CarrierService();
            Carrier carrier = new Carrier()
            {
                Address = string.Empty,
                CNPJ = string.Empty,
                Contact = string.Empty,
                ContactPhone = string.Empty,
                Name = "Transportadora " + name
            };
            carrierService.Create(carrier);


             OccurrenceReasonService occurrenceReasonService = new OccurrenceReasonService();
            OccurrenceReason occurrenceReason = new OccurrenceReason()
            {
               Name = "Motivo Ocorrencia " + name,
               EDICode = string.Empty,
               StandardClassificationId = 1,
               StandardCostCenter = string.Empty
            };
            occurrenceReasonService.Create(occurrenceReason);

         
            OccurrenceService occurrenceService = new OccurrenceService();
            Occurrence occurrence = new Occurrence();
            occurrence.Carrier = carrierService.FindAll().First();
            occurrence.OccurrenceDate = DateTime.Now;
            occurrence.OccurrenceReason = occurrenceReasonService.FindAll().First();
            occurrence.OccurrenceTypeId = 1;
            occurrence = occurrenceService.Create(occurrence);

            //ScheduledReturnWorflowControl workflow = new ScheduledReturnWorflowControl();
            WorkflowControl.CreateWorkflow(occurrence);            
        }
    }
}